/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mscs.cashier.logic;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import mscs.data.IFileDataStore;
import mscs.data.IGoodsDiscountProxy;
import mscs.data.IGoodsGiftProxy;
import mscs.data.IGoodsProxy;
import mscs.data.pojo.Goods;
import mscs.data.pojo.GoodsDiscount;
import mscs.data.pojo.GoodsGift;

/**
 *
 * @author Cake
 */
public class SaleGoodsUtil {
        
        List<Goods> goodsList= new ArrayList<Goods>();                  //待售商品
        List<GoodsGift> giftsList= new ArrayList<GoodsGift>();
        private List<Integer> numberList = new ArrayList<Integer>();  //对应销售商品的数量
        private List<Double> priceList = new ArrayList<Double>();     //每一条销售记录的总价（可能同一商品在先后录入）
         
	private IFileDataStore allgoods = new IGoodsProxy();
        private List<Goods> allgoodslist= (List<Goods>)allgoods.getObjects(); 
        
        private IFileDataStore allGoodsDiscount=new IGoodsDiscountProxy();
        private List<GoodsDiscount> allGoodsDiscountList= (List<GoodsDiscount>)allGoodsDiscount.getObjects();
        
        private IFileDataStore allGoodsGifts=new IGoodsGiftProxy();
        private List<GoodsGift> allGoodsGiftsList= (List<GoodsGift>)allGoodsGifts.getObjects();
        
       
        
        
        
        public SaleGoodsUtil(){
        
        }
      
     public List<GoodsGift> getGiftList(){
         return giftsList;
     }   
  
     public List<Double> getPriceList(){
         return priceList;
     }
        
     public List<Goods> getGoodsList(){
         return goodsList;
     }
    //更新搜索列表
     public Vector<List> searchTableVector(String key,String keywords) {
         return new Vector<List>(toSearchGoodsDataTable(allgoods.searchObjectsByAttr(key, ".*" + keywords + ".*")));
    }
     //更新售货列表
      public Vector<List> saleTableVector() {       
         return new Vector<List>(toSaleDataTable(goodsList));
    }
      
      public Vector<List> giftTabelVector(){
          return new Vector<List>(toGiftDataTable(giftsList));
      }
   
      
      
     
     //把一个商品加入到销售列表并更新库存信息          
      //同时获取赠品列表等信息
     public boolean addToGoodsList(Goods newSaleGoods,int num) {
         for(Goods goods:allgoodslist){     
             if((newSaleGoods.getId().equals(goods.getId()))&&(isEnough(goods,num))){ 
       
                 goods.setNum(goods.getNum()-num);
            
                 newSaleGoods.setNum(num);
                 newSaleGoods.setDesc(goods.getDesc());
                 newSaleGoods.setSalPrice(goods.getSalPrice());
                 
                 for(GoodsDiscount goodsdiscount:allGoodsDiscountList){
                    if(goodsdiscount.getId().equals(newSaleGoods.getId())&&(goodsdiscount.getEndDate().after(new Date()))&&(goodsdiscount.getStartDate().before(new Date()))){
                             this.priceList.add((double)num*goodsdiscount.getDiscountPrice());}
                       else{ this.priceList.add((double)num*newSaleGoods.getSalPrice());}
                     }
                 
                 for(GoodsGift goodsGift:allGoodsGiftsList){
                     if(goodsGift.getGoodsID().equals(newSaleGoods.getId())){
                         this.giftsList.add(goodsGift);
                     }
                 }
                 
                 this.numberList.add(num);
                 this.goodsList.add(newSaleGoods);
                                
                 update(goodsList);//销售记录提交的同时完成更新操作，，，记得更改这边
                 return true;
             }
         } 
         return false;
    }
     
     
     
     public boolean isEnough(Goods goods,int num){
         return goods.getNum()>=num;
     }
     
    //在后更新库存列表
    public void update(List<Goods> list) {  
        for(int i=0; i<allgoodslist.size(); i++){
            for(int j=0;j<list.size();j++){
                if(list.get(j).getId().equals(allgoodslist.get(i).getId())){
                   allgoods.update(allgoodslist);
               }
           }
        }
    }

    /*
     * 
     * 
    public String timeToString(Date date){
            
        String year=((date.getYear()-100)<10)?"0"+(date.getYear()-100):""+(date.getYear()-100);
        String month=(date.getMonth()+1)<10?"0"+(date.getMonth()+1):""+(date.getMonth()+1);
        String day=(date.getDay()+4)<10?"0"+(date.getDay()):""+date.getDay();
        String hour=date.getHours()<10?"0"+(date.getHours()):""+date.getHours();
        String minute=date.getMinutes()<10?"0"+(date.getMinutes()):""+date.getMinutes();
        String second=date.getSeconds()<10?"0"+(date.getSeconds()):""+date.getSeconds();
        String id=year+" "+month+" "+day+" "+hour+" "+minute+" "+second+" ";
        return id;
    }
     * 
     * 
     */
    
    
    
      public Vector<Vector<String>> toSearchGoodsDataTable(List list) {
    	try {
            Vector<Goods> seList = new Vector(list);
            Vector<Vector<String>> reList = new Vector<Vector<String>>();
            for (Goods goods : seList)
            {
                reList.add((Vector<String>)toSearchGoodsStringArray(goods));
            }
            return reList;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
      
       public Vector<Vector<String>> toSaleDataTable(List list) {
    	try {
            Vector<Goods> seList = new Vector(list);
            Vector<Vector<String>> reList = new Vector<Vector<String>>();
            for(int i=0; i<seList.size(); i++){
                reList.add((Vector<String>)toSaleStringArray(seList.get(i),numberList.get(i)));
            }
            return reList;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
       
        public Vector<Vector<String>> toGiftDataTable(List list) {
    	try {
            Vector<GoodsGift> seList = new Vector(list);
            Vector<Vector<String>> reList = new Vector<Vector<String>>();
            for(int i=0; i<seList.size(); i++){
                reList.add((Vector<String>)toGiftStringArray(seList.get(i)));
            }
            return reList;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    public List toGiftStringArray(GoodsGift gift){
        Vector<String> list = new Vector<String>();
        list.add(gift.getGiftID());
        
        for(Goods goods:allgoodslist){
            if(gift.getGoodsID().equals(goods.getId())){
                for(Goods good:allgoodslist){
                    if(gift.getGiftID().equals(good.getId())){
                         list.add(good.getDesc());
                    }
                }             
            }
        }
         
          
        list.add(String.valueOf(gift.getGiftSum()));
        return list;
    }
        
      
    
     public List toSearchGoodsStringArray(Goods goods){
        Vector<String> list = new Vector<String>();
        list.add(goods.getId());
        list.add(String.valueOf(goods.getNum()));
        list.add(goods.getDesc());
        list.add(((Double)goods.getSalPrice()).toString());
        return list;
    }
    
    public List toSaleStringArray(Goods goods, int num){
        Vector<String> list = new Vector<String>();
        list.add(goods.getId());
        list.add(String.valueOf(num));
        list.add(goods.getDesc());
        list.add(((Double)goods.getSalPrice()).toString());
        
        for(GoodsDiscount goodsdiscount:allGoodsDiscountList){
            if(goodsdiscount.getId().equals(goods.getId())&&(goodsdiscount.getEndDate().after(new Date()))&&(goodsdiscount.getStartDate().before(new Date()))){
                list.add(((Double)goodsdiscount.getDiscountPrice()).toString());
            }
            else list.add("");
        }
        
         for(GoodsDiscount goodsdiscount:allGoodsDiscountList){
            if(goodsdiscount.getId().equals(goods.getId())&&(goodsdiscount.getEndDate().after(new Date()))&&(goodsdiscount.getStartDate().before(new Date()))){
                list.add(((Double)(num*goodsdiscount.getDiscountPrice())).toString());
            }
            else list.add(String.valueOf(num*goods.getSalPrice()));;
        }
        
        return list;
    }
   
 
}
